//
//  BaseNavController.m
//  Kugou-OC
//
//  Created by BH on 16/5/10.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "BaseNavController.h"
#import "GlobalDefine.h"
#import "HomeController.h"
#import "BottomPlayView.h"

@implementation BaseNavController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBarTintColor:BG_COLOR];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18.f],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //显示bottomview
    for (UIView *subview in [UIApplication sharedApplication].keyWindow.subviews) {
        if ([subview isKindOfClass:[BottomPlayView class]]) {
            [UIView animateWithDuration:0 animations:^{
                subview.alpha = 1;
            } completion:^(BOOL finished) {
                subview.hidden = NO;
            }];
        }
    }
}

-(void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //只有一个返回按钮
    self.leftItems = @{@"img":@"top_back"};
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = YES;
}

#pragma mark - setter
-(void)setMiddleTitle:(NSString *)middleTitle{
    _middleTitle = middleTitle;
    
    self.title = middleTitle;
}

-(void)setLeftItems:(NSDictionary *)leftItems{
    _leftItems = leftItems;
    
    self.navigationItem.leftBarButtonItems = [self setButton:leftItems basicTag:100];
}

-(void)setRightItems:(NSDictionary *)rightItems{
    _rightItems = rightItems;
    
    self.navigationItem.rightBarButtonItems = [self setButton:rightItems basicTag:200];
}

-(NSArray *)setButton:(NSDictionary *)dict basicTag:(NSInteger)basicTag{
    
      NSArray *allkeys = [dict allKeys];
      NSArray *allvalues = [dict allValues];
      NSMutableArray *buttons = [NSMutableArray array];
      for (int i=0;i<allkeys.count;i++) {
          
          UIButton *button = ALLOC(UIButton);
          button.frame = CGRectMake(0, 0, 30, 30);
          button.tag = basicTag+i;
          [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
          
          if ([allkeys[i] hasPrefix:@"img"]) {
              //图片
              [button setImage:IMAGE(allvalues[i]) forState:UIControlStateNormal];
          }else{
              //文字
              button.frame = CGRectMake(0, 0, 60, 30);
              [button setTitle:allvalues[i] forState:UIControlStateNormal];
              [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
          }
          
          UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
          [buttons addObject:item];
      }
    
    return buttons;
                      
}

//返回
-(void)buttonClick:(UIButton *)button{
    if (button.tag == 100) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        
    }
}

@end
